﻿package bipede.user
{
	import events.UserEvent;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	import flash.geom.Rectangle;
	
	public class Voiture extends EventDispatcher
	{
		
		public var position:int;
		public var trm:int = 0;// tour minute [ 0 100 ]
		public var dir:int = 0;// tourne volant [ -10 +10 ]
		
		private var _engine:Timer;
		private var _left:Boolean=false;
		private var _right:Boolean=false;
		private var _up:Boolean=false;
		private var _down:Boolean=false;
		private var _espace:Boolean = false;
		private var _pilotable:Boolean = false;
		
		public var vitesse:int = 0;
		public var decalage:int = 0;

		public function Voiture()
		{
			//_engine = new Timer(30);
			//_engine.addEventListener(TimerEvent.TIMER, _moteur );
			
			//Main.instance.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			//Main.instance.stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);			
		}
		public function demarre():void
		{
			_engine.reset();
			_engine.start();
		}		
		public function set pilotable(p:Boolean):void
		{
			_pilotable = p;
			
			if ( _pilotable )
			{
				//stage.
				addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
				//stage.
				addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			}
			else {
				//if ( stage != null )
				{
					//stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
					//stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);		
				}
			}
		}
		public function get pilotable():Boolean
		{
			return _pilotable;
		}
        private function _moteur(event:TimerEvent):void
		{
			//if ( _left ) dir = Math.max( -10, dir - maniabilite );// tourne gauche
			//if ( _right ) dir = Math.min( 10, dir + maniabilite );// tourne droite
			//if ( !_left && !_right && dir != 0 ) dir -= dir / Math.abs( dir );// rappel volant
			//if ( _espace ) trm = Math.min( 100, trm + 1 );// accelere
			//else trm = Math.max( 0, trm - 2 );// decelere
			
			
		}
        public function keyDownHandler(event:KeyboardEvent):void
		{
			//trace(event);
			switch ( event.keyCode )
			{
				case Keyboard.LEFT : 
				_left = true;
				dispatchEvent( new UserEvent( UserEvent.LEFT ) );
				break;	
				case Keyboard.RIGHT : 
				_right = true;
				dispatchEvent( new UserEvent( UserEvent.RIGHT ) );
				break;	
				case Keyboard.UP : 
				_up = true;
				dispatchEvent( new UserEvent( UserEvent.UP ) );
				break;	
				case Keyboard.DOWN : 
				_down = true;
				dispatchEvent( new UserEvent( UserEvent.DOWN ) );
				break;					
				case Keyboard.SPACE : 
				_espace = true;
				dispatchEvent( new UserEvent( UserEvent.SPACE ) );
				break;
			}
        }
        public function keyUpHandler(event:KeyboardEvent):void
		{
			//trace(event);
			switch ( event.keyCode )
			{
				case Keyboard.LEFT : 
				dispatchEvent( new UserEvent( UserEvent.RELEASE_LEFT) );
				_left = false;
				break;	
				case Keyboard.RIGHT : 
				dispatchEvent( new UserEvent( UserEvent.RELEASE_RIGHT) );
				_right = false;
				break;	
				case Keyboard.UP : 
				dispatchEvent( new UserEvent( UserEvent.RELEASE_UP) );
				_up = false;
				break;	
				case Keyboard.DOWN : 
				dispatchEvent( new UserEvent( UserEvent.RELEASE_DOWN ) );
				_down = false;
				break;					
				case Keyboard.SPACE : 
				_espace = false;
				dispatchEvent( new UserEvent( UserEvent.RELEASE_SPACE ) );
				break;	
			}
        }
	}
}